package cn.fsw.pethome.product.service.impl;

import cn.fsw.pethome.base.service.impl.BaseServiceImpl;
import cn.fsw.pethome.product.domain.Product;
import cn.fsw.pethome.product.domain.ProductDetail;
import cn.fsw.pethome.product.domain.ProductOnlineAuditLog;
import cn.fsw.pethome.product.mapper.ProductDetailMapper;
import cn.fsw.pethome.product.mapper.ProductMapper;
import cn.fsw.pethome.product.mapper.ProductOnlineAuditLogMapper;
import cn.fsw.pethome.product.service.IProductService;
import cn.fsw.pethome.util.BaiduAuditUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fsw
 * @since 2022-10-27
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Override
    @Transactional
    public void add(Product product) {
        super.add(product);
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        productDetailMapper.add(detail);
    }

    @Override
    @Transactional
    public void update(Product product) {
        super.update(product);
        productDetailMapper.update(product.getDetail());
    }


    @Override
    public Product selectByDetail(Long id) {
        return productMapper.selectByDetail(id);
    }

    /*下架*/
    @Override
    @Transactional
    public void offsale(List<Long> ids) {
        Date date = new Date();
        productMapper.offsaleBatch(date,ids);
        List<ProductOnlineAuditLog> list = new ArrayList<>();
        for (Long id : ids) {
            ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
            productOnlineAuditLog.setAuditTime(date);
            productOnlineAuditLog.setProductId(id);
            productOnlineAuditLog.setState(1);
            productOnlineAuditLog.setNote("下架成功");
            list.add(productOnlineAuditLog);
        }
        productOnlineAuditLogMapper.addBatch(list);
    }

    @Override
    public Map<String, Object> onsale(List<Long> ids) {
        Map<String, Object> map = new HashMap<>();
        List<String> names = new ArrayList<>();
        for (Long id : ids) {
            /*记录日志*/
            Date date = new Date();
            ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
            productOnlineAuditLog.setAuditTime(date);
            productOnlineAuditLog.setProductId(id);
            /*当前上架的物品*/
            Product product = productMapper.selectById(id);
            /*审核*/
            if(BaiduAuditUtils.TextCensor(product.getName())){
                product.setState(1L);
                product.setOnsaletime(date);
                /*日志*/
                productOnlineAuditLog.setState(1);
                productOnlineAuditLog.setNote("上架成功");
                productMapper.update(product);

            }else {
                /*日志*/
                productOnlineAuditLog.setState(0);
                productOnlineAuditLog.setNote("审核失败");
                names.add(product.getName());
            }
            productOnlineAuditLogMapper.add(productOnlineAuditLog);
        }
        map.put("names",names);
        if(names.size() == 0){
            return null;
        }
        return map;
    }
}
